﻿using IncoolDicomViewerBase;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PubLib;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using xDataSet;

namespace IncoolReg
{
    public partial class MainForm : Form
    {
        public static xDataSetConfigInterface G_xDataSetConfigInterface;
         public static Hashtable G_BLL_Object;
        String mMainURL = "3dmed.net";
        string mAppID = "";
        String mDepId = "";
        String mDepName = "";
        String mProvinceId = "";
        String mProvinceName = "";
        String mCityId  = "";
        String mCityName = "";
        String mAreaId = "";
        String mAreaName = "";
        String mZuzhiId = "";
        String mZuzhiName = "";
        String mDepartmentId = "";
        String mDepartmentName = "";
        String mOfficeId = "";
        String mOfficeName = "";
        String mHospitailId = "";
        String mHospitailName = "";

        public MainForm()
        {
            InitializeComponent();
            G_xDataSetConfigInterface = new xDataSetConfigOnline();
            G_xDataSetConfigInterface.Appid = "1234567890";
            G_BLL_Object = new Hashtable();

        }

        string GetCpuID()
        {
            try
            {
                string cpuInfo = "";//cpu序列号 
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
                moc = null;
                mc = null;
                return cpuInfo;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        string GetMacAddress()
        {
            try
            {
                string mac = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        mac = mo["MacAddress"].ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return mac;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }



        //3.获取硬盘ID 
        string GetDiskID()
        {
            try
            {
                String HDid = "";
                ManagementClass mc = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    HDid = (string)mo.Properties["Model"].Value;
                }
                moc = null;
                mc = null;
                return HDid;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }





        //4.获取IP地址 

        string GetIPAddress()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        //st=mo["IpAddress"].ToString(); 
                        System.Array ar;
                        ar = (System.Array)(mo.Properties["IpAddress"].Value);
                        st = ar.GetValue(0).ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }



        /// 5.操作系统的登录用户名 
        string GetUserName()
        {
            try
            {
                string un = "";

                String st = Environment.UserName;
                return un;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }



        //6.获取计算机名
        string GetComputerName()
        {
            try
            {
                return System.Environment.MachineName;

            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }

        ///7 PC类型 
        string GetSystemType()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {

                    st = mo["SystemType"].ToString();

                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }

        /// 8.物理内存 

        string GetTotalPhysicalMemory()
        {

            string st = "";
            ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {

                st = mo["TotalPhysicalMemory"].ToString();

            }
            moc = null;
            mc = null;
            return st;

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            txt_sn.Text = GetCpuID();
            f_init();
        }

        void f_init()
        {
            //判断是否存在已经登记过的记录，如果记录已经存在的话，就不可以再次修改
            xDataSet.xDataSet mRetGetEquipment = new xDataSet.xDataSet("Weixin_Interface_User", "dev", "getEquipmentByEqID",
            new xParameter[] { new xParameter("id", txt_sn.Text) }, G_xDataSetConfigInterface)
            ;
            if (mRetGetEquipment.SRet)
            {
                //信息存在，显示这个机器的信息
                cb_ok.Visible = false;
                //List<Hashtable> mRetListGetEquipment = JsonConvert.DeserializeObject<List<Hashtable>>(mRetGetEquipment.SRetJObject["body"].ToString());
                //Hashtable mRetListGetEquipment = JsonConvert.DeserializeObject<Hashtable>(mRetGetEquipment.SRetJObject["body"].ToString());

                
                cmb_zhuzi.Text = mRetGetEquipment.SRetJObject["body"]["zuzhiName"].ToString();
                cmb_zhuzi.Enabled = false;

                cmb_hospital.Text = mRetGetEquipment.SRetJObject["body"]["hospitalName"].ToString();
                cmb_hospital.Enabled = false;

                cmb_dep.Text = mRetGetEquipment.SRetJObject["body"]["deparmentCategoryName"].ToString();
                cmb_dep.Enabled = false;

                cmb_office.Text = mRetGetEquipment.SRetJObject["body"]["officeName"].ToString();
                cmb_office.Enabled = false;

                cmb_province.Text = mRetGetEquipment.SRetJObject["body"]["provinceName"].ToString();
                cmb_province.Enabled = false;

                cmb_city.Text = mRetGetEquipment.SRetJObject["body"]["cityName"].ToString();
                cmb_city.Enabled = false;

                cmb_area.Text = mRetGetEquipment.SRetJObject["body"]["areaName"].ToString();
                cmb_area.Enabled = false;

            }
            else
            {

            }
            //获取省份的数据
            xDataSet.xDataSet mRet = new xDataSet.xDataSet("Weixin_Interface_User", "dev", "getAllZuZhi",
            new xParameter[] { new xParameter("id",1) }, G_xDataSetConfigInterface)
            ;
            cmb_zhuzi.DisplayMember = "aname";
            cmb_zhuzi.ValueMember = "id";
            //判断返回值是否正确
            if (mRet.SRetJObject["success"].ToString().ToLower() == "true")
            {
                //显示
                //cmb_province.Items.Add()
                List<Hashtable> mRetList = JsonConvert.DeserializeObject<List<Hashtable>>(mRet.SRetJObject["body"].ToString());
                for (int i = 0; i < mRetList.Count; i++)
                {
                    //添加每一条数据
                    cmb_zhuzi.Items.Add(new {
                        uuid = mRetList[i]["uuid"].ToString(),
                        aname = mRetList[i]["aname"],
                        id = mRetList[i]["id"].ToString(),
                        zipCode = mRetList[i]["zipCode"],
                        areaId = mRetList[i]["areaId"].ToString(),
                        areaName = mRetList[i]["areaName"],
                        provinceId = mRetList[i]["provinceId"].ToString(),
                        provinceName = mRetList[i]["provinceName"],
                        cityName = mRetList[i]["cityName"],
                        cityId = mRetList[i]["cityId"].ToString()
                    });
                }
            }


            //////////////////////////////
            //获取所有科室类别的数据
            xDataSet.xDataSet mRetDep = new xDataSet.xDataSet("Weixin_Interface_User", "dev", "getAllDepartment",
            new xParameter[] {  }, G_xDataSetConfigInterface)
            ;
            cmb_dep.DisplayMember = "name";
            cmb_dep.ValueMember = "id";
            //判断返回值是否正确
            if (mRetDep.SRetJObject["success"].ToString().ToLower() == "true") 
            {
                //显示
                //cmb_province.Items.Add()
                List<Hashtable> mRetList = JsonConvert.DeserializeObject<List<Hashtable>>(mRetDep.SRetJObject["body"].ToString());
                for (int i = 0; i < mRetList.Count; i++)
                {
                    //添加每一条数据
                    cmb_dep.Items.Add(new { id = mRetList[i]["id"].ToString(), name = mRetList[i]["name"] });
                }
            }
        }


        private void cb_ok_Click(object sender, EventArgs e)
        {
            //数据保存
            xDataSet.xDataSet mRetDep = new xDataSet.xDataSet("Weixin_Interface_User", "dev", "addEquipment",
           new xParameter[] {
               new xParameter("equipment",txt_sn.Text),
               new xParameter("equipment_id",txt_sn.Text),
               new xParameter("province_id",mProvinceId),
               new xParameter("province_name",mProvinceName),
               new xParameter("city_id",mCityId),
               new xParameter("city_name",mCityName),
               new xParameter("area_id",mAreaId),
               new xParameter("area_name",mAreaName),
               new xParameter("zuzhi_id",mZuzhiId),
               new xParameter("zuzhi_name",mZuzhiName),
               new xParameter("department_category_id",mDepartmentId),
               new xParameter("department_category_name",mDepartmentName),
               new xParameter("hospitail_id",mHospitailId),
               new xParameter("hospitail_name",mHospitailName),
               new xParameter("office_id",mOfficeId),
               new xParameter("office_name",mOfficeName)
           }, G_xDataSetConfigInterface)
           ;
            if (mRetDep.SRetJObject["success"].ToString().ToLower() == "true")
            {
                MessageBox.Show("保存成功");
            }

        }

        private void cmb_zhuzi_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取所属医院信息
            //MessageBox.Show(  ((List<Hashtable>)cmb_zhuzi.SelectedItem).ToString() );
            cmb_hospital.Items.Clear();
            cmb_hospital.Text = "";
            cmb_office.Items.Clear();
            cmb_office.Text = "";

            dynamic obj =cmb_zhuzi.SelectedItem;
            var name = obj.aname;
            var mId = obj.uuid;
            mZuzhiId =  obj.id;
            mZuzhiName = (string)obj.aname;
            mCityId = (string)obj.cityId;
            mCityName = (string)obj.cityName;
            mProvinceId = (string)obj.provinceId;
            mProvinceName = (string)obj.provinceName;
            mAreaId = (string)obj.areaId;
            mAreaName = (string)obj.areaName;

            cmb_province.Text = mProvinceName;
            cmb_city.Text = mCityName;
            cmb_area.Text = mAreaName;

            //uuid = mRetList[i]["uuid"],
            //aname = mRetList[i]["aname"],
            //id = mRetList[i]["id"],
            //zipCode = mRetList[i]["zipCode"],
            //areaId = mRetList[i]["areaId"],
            //areaName = mRetList[i]["areaName"],
            //provinceId = mRetList[i]["provinceId"],
            //provinceName = mRetList[i]["provinceName"],
            //cityName = mRetList[i]["cityName"],
            //cityId = mRetList[i]["cityId"]
            //处理参数

            //获取公司下医院信息
            xDataSet.xDataSet mRetHospital = new xDataSet.xDataSet("Weixin_Interface_User", "dev", "getHospitalByCompanyUUID",
            new xParameter[] { new xParameter("uuid", mId) }, G_xDataSetConfigInterface)
            ;
            cmb_hospital.DisplayMember = "title";
            cmb_hospital.ValueMember = "id";
            //判断返回值是否正确
            if (mRetHospital.SRetJObject["success"].ToString().ToLower() == "true")
            {
                //显示
                //cmb_province.Items.Add()
                List<Hashtable> mRetList = JsonConvert.DeserializeObject<List<Hashtable>>(mRetHospital.SRetJObject["body"].ToString());
                for (int i = 0; i < mRetList.Count; i++)
                {
                    //添加每一条数据
                    cmb_hospital.Items.Add(new { id = mRetList[i]["id"].ToString(), uuid = mRetList[i]["uuid"], title = mRetList[i]["title"] });
                }
            }

            
        }

        private void cmb_hospital_SelectedIndexChanged(object sender, EventArgs e)
        {
            //cmb_office
            dynamic obj = cmb_hospital.SelectedItem;
            var name = obj.title;
            var mId = obj.uuid;

            //处理选中的医院
            mHospitailId = obj.id;
            mHospitailName = obj.title;

            //获取公司下医院信息
            xDataSet.xDataSet mRetOfficel = new xDataSet.xDataSet("Weixin_Interface_User", "dev", "getOfficMsgByHospUUID",
            new xParameter[] { new xParameter("uuid", mId) }, G_xDataSetConfigInterface)
            ;
            cmb_office.DisplayMember = "title";
            cmb_office.ValueMember = "id";
            //判断返回值是否正确
            if (mRetOfficel.SRetJObject["success"].ToString().ToLower() == "true")
            {
                //显示
                //cmb_province.Items.Add()
                List<Hashtable> mRetList = JsonConvert.DeserializeObject<List<Hashtable>>(mRetOfficel.SRetJObject["body"].ToString());
                for (int i = 0; i < mRetList.Count; i++)
                {
                    //添加每一条数据
                    cmb_office.Items.Add(new { id = mRetList[i]["id"].ToString(), title = mRetList[i]["title"] });
                }
            }
        }

        private void cb_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmb_dep_SelectedIndexChanged(object sender, EventArgs e)
        {
            //处理部门类型数据
            dynamic obj = cmb_dep.SelectedItem;

            //处理选中的医院
            mDepartmentId = obj.id;
            mDepartmentName = obj.name;
        }

        private void cmb_office_SelectedIndexChanged(object sender, EventArgs e)
        {
            //处理科室类型数据
            dynamic obj = cmb_office.SelectedItem;

            //处理选中的医院
            mOfficeId = obj.id;
            mOfficeName = obj.title;
        }
    }
}
